<?php
/**
 * Created by PhpStorm.
 * User: DELL
 * Date: 2018/6/6
 * Time: 19:41
 */

namespace app\home\controller;
use app\home\model\WechatUser;
use app\home\model\Write;
use app\home\model\WriteSign;
use think\Db;
use com\wechat\TPWechat;
use think\Config;
/**
 * Class School
 * @package app\home\controller
 * 学生录入
 */
class School extends Base
{
    /*
     * 首页
     */
    public function index(){
        $userId = session('userId');
        $User = WechatUser::where(['openid' => $userId])->field('subscribe,review_status,openid,role,scan')->find();
        if ($User['review_status'] == 0 || $User['role'] == 1 || $User['subscribe'] == 0){
            $this->redirect('Journal/null');
        }
        $this->jssdk();
        $this->assign('is',$User['scan']);// 是否同意协议
        $this->assign('userid',$userId);
        $res = Write::where(['openid' => $userId])->find();
        if (empty($res)){
            // 未有记录
            // 分校
            $list = Db::name('fenxiao')->order('id','asc')->select();
            $this->assign('list',$list);
            // 班主任
            $name = Db::name('xxbteacher')->group('name')->order('name','asc')->select();
            $this->assign('name',$name);
            // 项目
            $project = Db::name('banxing')->field('xm')->group('xm')->order('xm','asc')->select();
            $this->assign('pro',$project);
            // 报班类型
            $ban = Db::name('banxing')->where('xm','考研')->order('id','asc')->select();
            $this->assign('ban',$ban);
            // 考试年份
            $y = date('Y')-1;
            $year = [];
            for ($i=0;$i<=4;$i++){
                array_push($year,$y+$i);
            }
            $this->assign('year',$year);
            // 入学年份
            $yy = date('Y')-5;
            $sch = [];
            for ($i=0;$i<=9;$i++){
                array_push($sch,$yy+$i);
            }
            $this->assign('sch',$sch);
            // 学校地区
            $area = Db::name('diqu')->order('dq_id','asc')->select();
            $this->assign('area',$area);
            // 学校
            $school = Db::table('xuexiao')->where('dq_id',12)->order('dx','asc')->select();
            $this->assign('school',$school);
            // 学院
            $home = Db::name('yxzy')->where('school_id',439)->group('xy')->select();
            $this->assign('home',$home);
            // 专业
            $zy = Db::name('yxzy')->where(['school_id' => 439,'xy' => '电子信息学院'])->order('zy','asc')->select();
            $this->assign('zy',$zy);
            // 抄送给 老师
            $teacher = WechatUser::where(['review_status' => 1,'role' => 1,'subscribe' => 1])->field('name,openid')->order('name')->select();
            $this->assign('teacher',$teacher);
            return $this->fetch();
        }else{
            // 已有记录
            $this->redirect('School/paper?openid='.$userId);
        }
    }
    /*
     * 有记录 页面
     */
    public function paper(){
        $this->jssdk();
        // 权限验证
        $userId = session('userId');
        // 获取数据
        $openid = input('openid');
        $User = WechatUser::where(['openid' => $userId])->field('subscribe,review_status,openid,role,is_sign,scan')->find();
        if ($User['review_status'] == 0 || $User['role'] == 1 || $User['subscribe'] == 0 || $userId != $openid){
            $this->redirect('Journal/null');
        }
        $info = Write::where(['openid' => $openid])->find();
        if ($info['qqqs']){
            $info['qqqs'] = json_decode($info['qqqs']);
        }
        $this->assign('userid',$openid);
        $this->assign('scan',$User['scan']);// 是否同意协议
        $this->assign('info',$info);  // 内容详情
        $this->assign('is',$User['is_sign']);  // 查看审批权限
        return $this->fetch();
    }
    /**
     * 重新填写
     */
    public function restart(){
        $this->jssdk();
        // 权限验证
        $userId = session('userId');
        $id = input('id');
        $info = Write::where(['id' => $id])->find();
        $User = WechatUser::where(['openid' => $userId])->field('subscribe,review_status,openid,role')->find();
        if ($User['review_status'] == 0 || $User['role'] == 1 || $User['subscribe'] == 0 || $userId != $info['openid']){
            $this->redirect('Journal/null');
        }
        // 分校
        $list = Db::name('fenxiao')->order('id','asc')->select();
        $this->assign('list',$list);
        // 班主任
        $name = Db::name('xxbteacher')->group('name')->order('name','asc')->select();
        $this->assign('name',$name);
        // 项目
        $project = Db::name('banxing')->field('xm')->group('xm')->order('xm','asc')->select();
        $this->assign('pro',$project);
        // 报班类型
        $ban = Db::name('banxing')->where('xm','考研')->order('id','asc')->select();
        $this->assign('ban',$ban);
        // 考试年份
        $y = date('Y')-1;
        $year = [];
        for ($i=0;$i<=4;$i++){
            array_push($year,$y+$i);
        }
        $this->assign('year',$year);
        // 入学年份
        $yy = date('Y')-5;
        $sch = [];
        for ($i=0;$i<=9;$i++){
            array_push($sch,$yy+$i);
        }
        $this->assign('sch',$sch);
        // 学校地区
        $area = Db::name('diqu')->order('dq_id','asc')->select();
        $this->assign('area',$area);
        // 学校
        $school = Db::table('xuexiao')->where('dq_id',12)->order('dx','asc')->select();
        $this->assign('school',$school);
        // 学院
        $home = Db::name('yxzy')->where('school_id',439)->group('xy')->select();
        $this->assign('home',$home);
        // 专业
        $zy = Db::name('yxzy')->where(['school_id' => 439,'xy' => '电子信息学院'])->order('zy','asc')->select();
        $this->assign('zy',$zy);
        // 抄送给 老师
        $teacher = WechatUser::where(['review_status' => 1,'role' => 1,'subscribe' => 1])->field('name,openid')->order('name')->select();
        $this->assign('teacher',$teacher);
        $this->assign('info',$info);  // 内容详情
        return $this->fetch();
    }
    /**
     * 根据项目筛选班型
     */
    public function sel(){
        $id = input('id');
        $list = Db::name('banxing')->where(['xm'=>['like','%'.$id.'%']])->order('id','asc')->select();
        return $list;
    }
    /** 
     * 根据地区选学校*/
    public function sele(){
        $id = input('id');
        $list = Db::table('xuexiao')->where('dq_id',$id)->field('dx,dx_id')->order('dx','asc')->select();
        return $list;
    }
    /**
     * 根据学校 选学院
     */
    public function selec(){
        $id = input('id');
        $list = Db::table('pb_yxzy')->where('school_id',$id)->group('xy')->select();
        return $list;
    }
    /**
     * 根据学校 学院 选择 专业
     */
    public function select(){
        $id = input('id'); // 学校 id
        $name = input('name'); // 学院
        $list = Db::table('pb_yxzy')->where(['school_id' => $id,'xy' => $name])->order('zy','asc')->select();
        return $list;
    }
    /**
     * 保存
     */
    public function save(){
        $userId = session('userId');
        $data = input('post.');
        $data['openid'] = $userId;
        $data['status'] = 0; // 待审核
        $data['create_time'] = time();
        $Model = new Write();
        if ($data['time']){
            $data['time'] = strtotime($data['time']);  // 时间转化
        }
        if ($data['qqqs']){
            $data['qqqs'] = json_encode($data['qqqs']);
        }
        $result = $Model->where(['openid' => $userId])->find();
        if (empty($result)){
            // 新增
            $res = $Model->save($data);
        }else{
            // 修改
            unset($data['openid']);
            $res = $Model->save($data,['openid' => $userId]);
        }
        if ($res){
            $con = '【温馨提示】
学员信息录入更新了，请点击<a href="http://mp.csppat.com/DDDeducation/public/index.php/home/Write/index">信息录入</a>查看并审批~~';
            $send = array(
                "content" => $con
            );
            $Wechat = new TPWechat(Config::get('party'));
            $data = array(
                "touser" => $data['teacherid'],
                "msgtype" => "text",
                "text" => $send
            );
            $Wechat->sendMassMessage($data);
            return $this->success('提交成功');
        }else{
            return $this->error('提交失败');
        }
    }
    /**
     * 查看批阅
     */
    public function sign(){
        $userId = session('userId');
        $User = WechatUser::where(['openid' => $userId])->field('subscribe,review_status,role,school_id')->find();
        $id = input('id');
        $openid = Write::where(['id' => $id])->value('openid');
        if ($User['review_status'] == 0 || $User['role'] == 1 || $User['subscribe'] == 0 || $openid != $userId){
            $this->redirect('Journal/null');
        }
        $this->jssdk();
        $list = WriteSign::where(['wid' => $id,'openid' => $openid])->order('create_time','desc')->limit(15)->select();
        $this->assign('detail',$list); // 批阅列表
        $this->assign('id',$id);
        return $this->fetch();    
    }
    /**
     * 遵守规定
     */
    public function play(){
        $userid = input('userid');
        $res = WechatUser::where('openid',$userid)->update(['scan' => 1]);
        if ($res){
            return $this->success('操作成功');
        }else{
            return $this->error('操作失败');
        }
    }
}